home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #2 / Amiga Plus CD - 1995 - No. 2.iso / startrek / trek73 / src / firing.c < prev    next >
C/C++ Source or Header  |  1995-04-11  |  5KB  |  254 lines

  1. /*
  2.  * TREK73: firing.c
  3.  *
  4.  * Take care of firing phasers and torpedos for both enemy
  5.  * and fed ships.
  6.  *
  7.  * phaser_firing, torpedo_firing, ship_detonate, torp_detonate
  8.  *
  9.  */
  10.  
  11. #include "defines.h"
  12. #include "structs.h"
  13.  
  14. extern struct ship *shiplist[];
  15. extern char shutup[];
  16.  
  17. phaser_firing(sp)
  18. struct ship *sp;
  19. {
  20.     extern    struct list head;
  21.     extern    struct list *tail;
  22.     extern    struct damage p_damage;
  23.     register int i;
  24.     register int j;
  25.     int    hit;
  26.     struct    ship *ep;
  27.     struct    torpedo *tp;
  28.     int    s;
  29.     int    x, y;
  30.     struct    ship *target;
  31.     struct    list *lp;
  32.     int    bear;
  33.     struct     ship *fed;
  34.  
  35.  
  36.     fed = shiplist[0];
  37.     for (i=0; i<4; i++) {
  38.         if (sp->phasers[i].status & P_FIRING)
  39.             break;
  40.     }
  41.     if (i == 4)
  42.         return 0;
  43.     sp->phasers[i].status &= ~P_FIRING;
  44.     target = sp->phasers[i].target;
  45.     /*
  46.      * Put in j the exact bearing of the phasers relative to
  47.      * the ship
  48.      */
  49.     if (target == NULL) {
  50.         bear = sp->phasers[i].bearing + sp->course;
  51.         j = sp->phasers[i].bearing;
  52.     } else {
  53.         bear = bearing(sp->x, target->x, sp->y, target->y);
  54.         j = bear - sp->course;
  55.     }
  56.     j = rectify(j);
  57.     if (j > 125 && j < 235 && !(sp->status & S_ENG))
  58.         return 0;
  59.     if (target != NULL && (target->status & S_DEAD)) {
  60.         if ((sp = fed) && (!shutup[PHASERS+j])&& !(sp->status & S_DEAD))
  61.             printf("%s phaser %d disengaging\n", sp->name, i+1);
  62.         sp->phasers[i].target = NULL;
  63.         shutup[PHASERS+j]++;
  64.         return 0;
  65.     }
  66.     if (target != NULL)
  67.         sp->phasers[i].bearing = j;
  68.     printf(" <%s frng phasers>\n", sp->name);
  69.     for (lp = &head; lp != tail; lp = lp->fwd) {
  70.         if (lp->type == 0)
  71.             continue;
  72.         ep = NULL;
  73.         tp = NULL;
  74.         if (lp->type == I_SHIP) {
  75.             ep = lp->data.sp;
  76.             if (ep == sp)
  77.                 continue;
  78.             x = ep->x;
  79.             y = ep->y;
  80.         } else {
  81.             tp = lp->data.tp;
  82.             x = tp->x;
  83.             y = tp->y;
  84.         }
  85.         hit = phaser_hit(sp, x, y, &sp->phasers[i], bear);
  86.         if (hit == 0)
  87.             continue;
  88.         if (tp) {
  89.             if (tp->timedelay > 2) {
  90.                 switch (lp->type) {
  91.                 default:
  92.                 case I_SHIP:
  93.                     printf("oops...\n");
  94.                     break;
  95.                 case I_TORPEDO:
  96.                     printf("hit on torpedo %d\n",
  97.                         tp->id);
  98.                     break;
  99.                 case I_ENG:
  100.                     printf("%s's engineering hit.\n",
  101.                         tp->from->name);
  102.                     break;
  103.                 case I_PROBE:
  104.                     printf("hit on probe %d\n", 
  105.                         tp->id);
  106.                     break;
  107.                 }
  108.                 tp->timedelay = 2;
  109.             }
  110.             tp->fuel -= hit/2;
  111.             if (tp->fuel < 0)
  112.                 tp->fuel = 0;
  113.             continue;
  114.         }
  115.         /*
  116.          * Determine which shield was hit
  117.          */
  118.         j = rectify(bearing(x, sp->x, y, sp->y) - ep->course);
  119.         if (j > 315 || j < 45)
  120.             s = 1;
  121.         else if (j < 135)
  122.             s = 2;
  123.         else if (j < 225)
  124.             s = 3;
  125.         else
  126.             s = 4;
  127.         damage(hit, ep, s, &p_damage);
  128.     }
  129.     /*
  130.      * Reduce the load by the firing percentage
  131.      */
  132.     sp->phasers[i].load = sp->phasers[i].load
  133.         - sp->phasers[i].load * sp->p_percent / 100;
  134.     return 0;
  135. }
  136.  
  137. torpedo_firing(sp)
  138. struct ship *sp;
  139. {
  140.     extern    struct damage a_damage;
  141.     extern    struct list *newitem();
  142.     register int i;
  143.     register int j;
  144.     register int th;
  145.     struct    torpedo *tp;
  146.     struct    ship *target;
  147.     struct    list *lp;
  148.     int    bear;
  149.     struct    ship *fed;
  150.  
  151.  
  152.     fed = shiplist[0];
  153.     for (i=0; i<6; i++) {
  154.         if (sp->tubes[i].status & T_FIRING)
  155.             break;
  156.     }
  157.     if (i == 6)
  158.         return 0;
  159.     sp->tubes[i].status &= ~T_FIRING;
  160.     th = sp->tubes[i].load;
  161.     if (th == 0)
  162.         return 0;
  163.     target = sp->tubes[i].target;
  164.     /*
  165.      * Put in j the relative bearing of the tube
  166.      */
  167.     if (target == NULL) {
  168.         bear = sp->tubes[i].bearing + sp->course;
  169.         j = sp->tubes[i].bearing;
  170.     } else {
  171.         bear = bearing(sp->x, target->x, sp->y, target->y);
  172.         j = bear - sp->course;
  173.     }
  174.     j = rectify(j);
  175.     if (j > 125 && j < 235 && !(sp->status & S_ENG))
  176.         return 0;
  177.     if (target != NULL && (target->status & S_DEAD)) {
  178.         if ((sp = fed) && (!shutup[TUBES+j])&&!(sp->status & S_DEAD))
  179.             printf("   tube %d disengaging\n", i+1);
  180.         sp->tubes[i].target = NULL;
  181.         shutup[TUBES+j]++;
  182.         return 0;
  183.     }
  184.     if (target != NULL)
  185.         sp->tubes[i].bearing = j;
  186.     sp->tubes[i].load = 0;
  187.     lp = newitem(I_TORPEDO);
  188.     lp->type = I_TORPEDO;
  189.     lp->data.tp = MKNODE(struct torpedo, *, 1);
  190.     tp = lp->data.tp;
  191.     tp->from = sp;
  192.     tp->x = sp->x;
  193.     tp->y = sp->y;
  194.     tp->target = NULL;
  195.     tp->course = rectify(bear);
  196.     tp->fuel = th;
  197.     tp->speed = sp->t_lspeed + sp->warp;
  198.     tp->newspeed = tp->speed;
  199.     tp->timedelay = sp->t_delay * 10;
  200.     tp->prox = sp->t_prox;
  201.     tp->id = new_slot();
  202.     printf(" <<%s frng torpedo %d>>\n", sp->name, tp->id);
  203.     return 1;
  204. }
  205.  
  206. int ship_detonate(sp, lp)
  207. struct ship *sp;
  208. struct list *lp;
  209. {
  210.     register int fuel;
  211.     register int i;
  212.  
  213.     fuel = 0;
  214.     printf("++%s++ destruct.\n", sp->name);
  215.     for (i=0; i<4; i++)
  216.         if (sp->phasers[i].status & ~P_DAMAGED)
  217.             fuel += min(sp->phasers[i].load, 10);
  218.     for (i=0; i<6; i++)
  219.         if (sp->tubes[i].status & ~T_DAMAGED)
  220.             fuel += min(sp->tubes[i].load, 10);
  221.     fuel += sp->pods;
  222.     antimatter_hit((char *) sp, sp->x, sp->y, fuel);
  223.     lp->type = 0;
  224.     sp->status |= S_DEAD;
  225. }
  226.  
  227.  
  228. int torp_detonate(tp, lp)
  229. struct torpedo *tp;
  230. struct list *lp;
  231. {
  232.  
  233.     switch (lp->type) {
  234.         case I_SHIP:
  235.             printf("we aren't supposed to be here \n");
  236.             break;
  237.         case I_TORPEDO:
  238.             printf(":: torp %d ::\n", tp->id);
  239.             break;
  240.         case I_PROBE:
  241.             printf("** probe %d **\n", tp->id);
  242.             break;
  243.         case I_ENG:
  244.             printf("## %s engineering ##\n", tp->from->name);
  245.             break;
  246.         default:
  247.             printf("what the heck is this\n");
  248.             break;
  249.     }
  250.     antimatter_hit((char *) tp, tp->x, tp->y, tp->fuel);
  251.     return_slot(tp->id);
  252.     delitem(lp);
  253. }
  254.